--[[ 
	Current_Cooldowns 2.0 by monogato
	
	GetCurrentCd(iIndex, iSpell) returns the current cooldown of any champion spell.
	
	Note: Call CurrentCooldowns__OnLoad() in your script´s OnLoad().
]]

-- Spell data
ChampIDs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
QNames = {"", "", "", "", "", "", "", "", "", ""}
WNames = {"", "", "", "", "", "", "", "", "", ""}
ENames =  {"", "", "", "", "", "", "", "", "", ""}
RNames =  {"", "", "", "", "", "", "", "", "", ""}
Summoner1Names =  {"", "", "", "", "", "", "", "", "", ""}
Summoner2Names =  {"", "", "", "", "", "", "", "", "", ""}
-- Cast track
lastQs = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}
lastWs = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}
lastEs = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}
lastRs = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}
lastSummoner1s = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}
lastSummoner2s = {-300, -300, -300, -300, -300, -300, -300, -300, -300, -300}

function FillChampIDs()
	for i=1, heroManager.iCount do
		ChampIDs[i] = heroManager:GetHero(i).networkID
	end
end

function FillSpellNames()
	local champion
	
	for i=1, heroManager.iCount do
		champion = heroManager:GetHero(i)
		QNames[i] = champion:GetSpellData(_Q).name
		WNames[i] = champion:GetSpellData(_W).name
		ENames[i] = champion:GetSpellData(_E).name
		RNames[i] = champion:GetSpellData(_R).name
		Summoner1Names[i] = champion:GetSpellData(SUMMONER_1).name
		Summoner2Names[i] = champion:GetSpellData(SUMMONER_2).name
	end
end

function CurrentCooldowns__OnLoad()
	FillChampIDs()
	FillSpellNames()
end

function GetiIndex(champion)
	local champion_id = champion.networkID
	local i = 1
	local iIndex
	local found = false
	
	while not found do
		if ChampIDs[i] == champion_id then
			iIndex = i
			found = true
		end
		
		i = i + 1
	end
	
	return iIndex
end

function GetiSpell(iIndex, spell)
	local spell_name = spell.name
	local iSpell
	
	if spell_name == QNames[iIndex] then iSpell = _Q
	elseif spell_name == WNames[iIndex] then iSpell = _W
	elseif spell_name == ENames[iIndex] then iSpell = _E
	elseif spell_name == RNames[iIndex] then iSpell = _R
	elseif spell_name == Summoner1Names[iIndex] then iSpell = SUMMONER_1
	elseif spell_name == Summoner2Names[iIndex] then iSpell = SUMMONER_2
	end
	
	return iSpell
end

function OnProcessSpell(unit, spell)
	if unit.type == "obj_AI_Hero" and string.find(spell.name, "Basic") == nil then
		local iIndex = GetiIndex(unit)
		local iSpell = GetiSpell(iIndex, spell)
		
		if iSpell == _Q then lastQs[iIndex] = GetGameTimer()
		elseif iSpell == _W then lastWs[iIndex] = GetGameTimer()
		elseif iSpell == _E then lastEs[iIndex] = GetGameTimer()
		elseif iSpell == _R then lastRs[iIndex] = GetGameTimer()
		elseif iSpell == SUMMONER_1 then lastSummoner1s[iIndex] = GetGameTimer()
		elseif iSpell == SUMMONER_2 then lastSummoner2s[iIndex] = GetGameTimer()
		end
	end
end

function GetCurrentCd(iIndex, iSpell)
	local champion = heroManager:GetHero(iIndex)
	local lastCast
	local spellCd
	local currentCd
	
	if champion:CanUseSpell(iSpell) ~= COOLDOWN then
		currentCd = 0
	else
		if iSpell == _Q then
			lastCast = lastQs[iIndex]
			spellCd = champion:GetSpellData(_Q).cd * (1 + champion.cdr)
		elseif iSpell == _W then
			lastCast = lastWs[iIndex]
			spellCd = champion:GetSpellData(_W).cd * (1 + champion.cdr)
		elseif iSpell == _E then
			lastCast = lastEs[iIndex]
			spellCd = champion:GetSpellData(_E).cd * (1 + champion.cdr)
		elseif iSpell == _R then
			lastCast = lastRs[iIndex]
			spellCd = champion:GetSpellData(_R).cd * (1 + champion.cdr)
		elseif iSpell == SUMMONER_1 then
			lastCast = lastSummoner1s[iIndex]
			spellCd = champion:GetSpellData(SUMMONER_1).cd
		elseif iSpell == SUMMONER_2 then
			lastCast = lastSummoner2s[iIndex]
			spellCd = champion:GetSpellData(SUMMONER_2).cd
		end
		
		currentCd = spellCd - (GetGameTimer() - lastCast)
		if currentCd < 0 then currentCd = 0 end
	end
	
	return math.floor(currentCd)
end

--UPDATEURL=
--HASH=0C0BF798992028005257BBC3B4BB8489
